_ = require 'underscore'
Path = require 'path'
Fs = require 'fs'
Config = require '../config'

ImportFilesObjs = {}

staticPrefix = Config.server.staticPrefix
version = new Date().getTime()
MergerFiles = 
  getMergerFile : (file, type) ->
    self = @
    mergerFile = ''
    if type is 'css'
      searchFiles = self.cssList
    else
      searchFiles = self.jsList
    _.each searchFiles, (files) ->
      if not mergerFile && (_.indexOf files, file) isnt -1
        mergerFile = files[0]
    return mergerFile
  js : [
    {
      # combine :
      #   file : '/mergers/combine.min.js'
      cookies : 
        file : '/javascripts/jquery/jquery.cookies.min.js'
        index : 0
      url : 
        file : '/javascripts/jquery/jquery.url.min.js'
        index : 1
    }
  ]
  css : [
    # {
    #   global : 
    #     file : '/stylesheets/global.css'
    #     index : 0
    #   header : 
    #     file : '/stylesheets/header.css'
    #     index : 1
    # }
  ]

_.each MergerFiles, (mergerInfo, mergerType) ->
  if _.isArray mergerInfo
    mergeList = []
    _.each mergerInfo, (mergers) ->
      fileName = 'vicanso'
      combineFileName = ''
      filePathList = []
      _.each mergers, (fileInfo, key) ->
        fileName += "_#{key}"
        if key is 'combine'
          combineFileName = fileInfo.file
        else if fileInfo.index?
          filePathList[fileInfo.index] = fileInfo.file
      if filePathList.length isnt 0
        if combineFileName
          fileName = combineFileName
        else
          fileName = "/mergers/#{fileName}.#{mergerType}"
        mergeList.push [fileName].concat _.compact filePathList
    _.each mergeList, (files) ->
      filePath = "#{Path.dirname __dirname}/public/"
      saveFile = Path.join filePath, files[0]
      content = ''
      _.each files, (file, i) ->
        if i isnt 0
          content +=  Fs.readFileSync Path.join filePath, file
      Fs.writeFileSync saveFile, content
    MergerFiles["#{mergerType}List"] = mergeList
delete MergerFiles.js
delete MergerFiles.css

class ImportFiles
  ###*
   * [constructor description]
   * @param  {[type]} debug [description]
   * @return {[type]}       [description]
  ###
  constructor : (debug) ->
    @.cssFiles = []
    @.jsFiles = []
    @.debug = debug
    @.cssReady = false
    @.jsReady = false
  ###*
   * [importCss description]
   * @param  {[type]} path    [description]
   * @param  {[type]} prepend [description]
   * @return {[type]}         [description]
  ###
  importCss : (path, prepend) ->
    self = @
    if self.cssReady and not self.debug
      return self
    self.importFiles path, 'css', prepend
    return self
  ###*
   * [importJs description]
   * @param  {[type]} path    [description]
   * @param  {[type]} prepend [description]
   * @return {[type]}         [description]
  ###
  importJs : (path, prepend) ->
    self = @
    if self.jsReady and not self.debug
      return self
    self.importFiles path, 'js', prepend
    return self
  ###*
   * [importFiles description]
   * @param  {[type]} path    [description]
   * @param  {[type]} type    [description]
   * @param  {[type]} prepend [description]
   * @return {[type]}         [description]
  ###
  importFiles : (path, type, prepend) ->
    self = @
    if _.isString path
      path = path.trim()
      if not self.debug
        mergerFile = MergerFiles.getMergerFile path, type
        if mergerFile
          path = mergerFile
      if type is 'css'
        if (_.indexOf self.cssFiles, path) is -1
          if prepend
            self.cssFiles.unshift path
          else
            self.cssFiles.push path
      else if (_.indexOf self.jsFiles, path) is -1
        if prepend
          self.jsFiles.unshift path
        else
          self.jsFiles.push path
    else if _.isArray path
      if prepend
        path.reverse()
      _.each path, (item) ->
        self.importFiles item, type, prepend
    return self
  ###*
   * [exportCss description]
   * @return {[type]} [description]
  ###
  exportCss : () ->
    self = @
    cssFileList = []
    _.each self.cssFiles, (cssFile) ->
      if cssFile.indexOf('http') isnt 0
        cssFile = Path.join(staticPrefix, cssFile) + "?version=#{version}"
      cssFileList.push '<link rel="stylesheet" href="' + cssFile + '" type="text/css" media="screen" />'
    self.cssReady = true
    return cssFileList.join ''
  ###*
   * [exportJs description]
   * @return {[type]} [description]
  ###
  exportJs : () ->
    self = @
    jsFileList = []
    _.each self.jsFiles, (jsFile) ->
      if self.debug
        jsFile = ('' + jsFile).replace '.min.js', '.js'
      if jsFile.indexOf('http') isnt 0
        jsFile = Path.join(staticPrefix, jsFile) + "?version=#{version}"
      jsFileList.push '<script type="text/javascript" src="' + jsFile + '"></script>'
    self.jsReady = true
    return jsFileList.join ''
    

###*
 * [getImportObj description]
 * @param  {[type]} view [description]
 * @return {[type]}      [description]
###
module.exports.getImportObj = (view) ->
  if _.isString view
    if ImportFilesObjs[view]?
      return ImportFilesObjs[view]
    else
      return ImportFilesObjs[view] = new ImportFiles Config.debug
  else
    return new ImportFiles Config.debug